// 根据端处理事件
export const handleClientEvent = ({ MP_WEIXIN, OA_WEIXIN, H5, APP, MP_TOUTIAO, OTHER }) => {
  let func = OTHER()
  // #ifdef MP-WEIXIN
  func = MP_WEIXIN()
  // #endif

  // #ifdef H5
  func = isWeixinClient() ? OA_WEIXIN() : H5()
  // #endif

  // #ifdef APP-PLUS
  func = APP()
  // #endif

  // #ifdef MP-TOUTIAO
  func = MP_TOUTIAO()
  // #endif
  return func
}

export const wxpay = async (options) => {
  await handleClientEvent({
    // 微信小程序
    MP_WEIXIN: () => {
      return new Promise((resolve, reject) => {
        console.log(options, 'weixin')
        uni.requestPayment({
          provider: 'wxpay',
          timeStamp: options.timeStamp,
          // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
          nonceStr: options.nonceStr,
          // 支付签名随机串，不长于 32 位
          package: options.package,
          // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=***）
          signType: options.signType,
          // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
          paySign: options.paySign,
          success: res => resolve(res),
          cancel: res => reject(res),
          fail: res => reject(res),
        })
      })
    },

    // 微信公众号
    OA_WEIXIN: () => {
      return wxOaPay(options)
    },

    // H5
    H5: () => {
      return new Promise((resolve, reject) => {
        const url = `${options}&redirect_url=${encodeURIComponent(`${window.location.href}&showPop=1`)}`
        window.open(url, '_self')
      })
    },

    // APP
    APP: () => {
      return new Promise((resolve, reject) => {
        uni.requestPayment({
          provider: 'wxpay',
          orderInfo: options,
          success: res => resolve(res),
          cancel: res => reject(res),
          fail: res => reject(res),
        })
      })
    },
  })
}

// 支付宝支付
export const alipay = async (options) => {
  await handleClientEvent({
    // H5
    H5: () => {
      return new Promise((resolve, reject) => {
        const alipayPage = window.open('', '_self')
        alipayPage.document.body.innerHTML = options
        alipayPage.document.forms[0].submit()
        resolve()
      })
    },

    // APP
    APP: () => {
      return new Promise((resolve, reject) => {
        uni.requestPayment({
          provider: 'alipay',
          orderInfo: options,

          success: res => resolve('success'),
          cancel: res => reject('fail'),
          fail: res => reject('fail'),
        })
      })
    },
    MP_TOUTIAO: () => {
      return new Promise((resolve, reject) => {
        uni.requestPayment({
          provider: 'alipay',
          orderInfo: options,
          service: 1,
          success: res => resolve(res),
          cancel: res => reject(res),
          fail: res => reject(res),
        })
      })
    },
  })
}

export const ttpay = async (options) => {
  return new Promise((resolve, reject) => {
    tt.pay({
      orderInfo: options,
      service: 5,
      success: (res) => {
        if (res.code == 0) {
          resolve(res)
        }
        else {
          reject(res)
        }
      },
      cancel: res => reject(res),
      fail: res => reject(res),
    })
  })
}
